package com.yegs.jltqc.serviceImpl;

import com.yegs.jltqc.TO.NamePageQuery;
import com.yegs.jltqc.VO.QcProduceWorkFlow;
import com.yegs.jltqc.VO.WorkFlow;
import com.yegs.jltqc.VO.WorkFlowResult;
import com.yegs.jltqc.common.StoreException;
import com.yegs.jltqc.config.ConcurentCache;
import com.yegs.jltqc.dao.WorkFlowRepository;
import com.yegs.jltqc.dao.WorkFlowResultRepository;
import com.yegs.jltqc.service.WorkFlowResultService;
import com.yegs.jltqc.service.WorkFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WorkFlowResultServiceImpl implements WorkFlowResultService {

    @Autowired
    private WorkFlowResultRepository workFlowResultRepository;

    @Autowired
    private WorkFlowRepository workFlowRepository;

    @Override
    public WorkFlowResult save(WorkFlowResult workFlowResult) {
        String departmentName = ConcurentCache.getLoginUser().getDepartmentName();
        WorkFlowResult result = workFlowResultRepository.findWorkFlowResultByRefIdAndUserType(workFlowResult.getRefId(), departmentName);
        WorkFlow workFlow = workFlowRepository.getOne(workFlowResult.getRefId());
        if(workFlowResultRepository.findWorkFlowResultByRefIdAndUserType(workFlow.getRefId(),
                QcProduceWorkFlow.getNext(workFlowResult.getUserType())) != null){
            throw new StoreException("下游部门已审核，不能修改!");
        }
        if(JUDGE_REJECT.equals(workFlowResult.getJudge())){
            workFlow.setResult(WorkFlow.RESULT_REJECT);
            workFlow.setStatus(WorkFlow.STATUS_END);
            workFlow.setUserType(QcProduceWorkFlow.getNext(ConcurentCache.getLoginUser().getDepartmentName()));
        }else{
            workFlow.setUserType(departmentName);
            if(departmentName.equals(QcProduceWorkFlow.getLast())){
                workFlow.setStatus(WorkFlow.STATUS_END);
                workFlow.setRemark(workFlowResult.getRemark());
                workFlow.setResult(workFlow.getResult());
            }else{
                workFlow.setStatus(WorkFlow.STATUS_ING);
                workFlow.setResult(null);
            }
        }
        if(result == null){
            workFlowResult.setCreateTime(new Date());
            workFlowResult.setCreateBy(ConcurentCache.getUserName());
            workFlowResult.setUserType(departmentName);
            workFlowRepository.save(workFlow);
            return workFlowResultRepository.save(workFlowResult);
        }
        else{
            result.setJudge(workFlowResult.getJudge());
            result.setRemark(workFlowResult.getRemark());
            workFlowRepository.save(workFlow);
            return workFlowResultRepository.save(result);
        }
    }

    @Override
    public List<WorkFlowResult> list() {
        return workFlowResultRepository.findAll();
    }

}
